package org.wicketskunkworks.shoebox.badging;

import org.apache.wicket.IResourceListener;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.WebMarkupContainer;

public class BadgingContainer extends WebMarkupContainer {
	private ResourceReference reference;

	public BadgingContainer(String id, final String label) {
		super(id);
		// Obtain the original image resource reference
		// ResourceReference icon = ...
		// if (hasError) {
		// // Error? Let's use the CompositeImageResource with our icon
		// // and the static ERROR decoration and name accordingly the
		// // ResourceReference
		// this.reference = new DynamicResourceReference(icon.getName()
		// + "_error", new CompositeImageResource(icon, ERROR));
		// } else {
		// // No error? let's stick to the source resource reference
		// this.reference = icon;
		// }

		// XXX What's the name? a param set so that its unique?
		this.reference = new WrapperResourceReference("",
		        new BadgingImageResource(label));
	}

	@Override
	protected void onComponentTag(ComponentTag tag) {
		super.onComponentTag(tag);
		// Obtain an url to our image, wheter it be the original icon
		// or the generated image
		CharSequence url = reference != null ? RequestCycle.get().urlFor(
		        reference) : this.urlFor(IResourceListener.INTERFACE);

		tag.put("style", "background-image: url('"
		        + RequestCycle.get().getOriginalResponse().encodeURL(url)
		        + "')");
	}

}
